package com.yantong.service.impl;

import com.yantong.mapper.GoodsPicMapper;
import com.yantong.pojo.GoodsPic;
import com.yantong.service.GoodsPicService;
import com.yantong.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @date 2018/8/29.13:45
 */
@Service
public class GoodsPicServiceImpl implements GoodsPicService {

    private static final Logger logger = LoggerFactory.getLogger(GoodsPicServiceImpl.class);

    @Autowired
    private GoodsPicMapper goodsPicMapper;


    @Override
    public PageUtils<GoodsPic> listAdverts(String advName, Long centreId, int offset, int limit) {
        logger.info("listAdverts advName:{}", advName);
        PageUtils<GoodsPic> pageUtils = new PageUtils<>();
        Map<String,Object> params = new HashMap<>(4);

        if (StringUtils.isNotEmpty(advName)) {
            params.put("advName",advName);
        }
        if (centreId != null) {
            params.put("centreId",centreId);
        }

        int total = goodsPicMapper.countByParams(params);
        if (total > 0) {
            List<GoodsPic> list = goodsPicMapper.listByParams(params, new RowBounds(offset, limit));
            pageUtils.setRows(list);
            pageUtils.setTotal(total);
        }
        return pageUtils;
    }

    @Override
    public int saveAdvert(GoodsPic advert) {
        if (advert == null) {
            return 0;
        }
        return goodsPicMapper.insertSelective(advert);
    }

    @Override
    public GoodsPic getByKey(Long id) {
        if (id == null) {
            return null;
        }
        GoodsPic goodsPic=goodsPicMapper.selectByPrimaryKey(id.intValue());
        //时间处理
        if(goodsPic.getUpStartTime() != null){
            goodsPic.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsPic.getUpStartTime()));
        }
        if(goodsPic.getUpEndTime() != null){
            goodsPic.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsPic.getUpEndTime()));
        }
        return goodsPic;
    }

    @Override
    public int updateAdvert(GoodsPic advert) {
        if (advert == null || advert.getId() == null) {
            return 0;
        }

        return goodsPicMapper.updateByPrimaryKeySelective(advert);
    }

    @Override
    public int removeAdvert(Integer advertId) {
        if (advertId == null) {
            return 0;
        }
        GoodsPic goodsPic = new GoodsPic();
        goodsPic.setId(advertId);
        goodsPic.setIsdelete(1);
        return goodsPicMapper.updateByPrimaryKeySelective(goodsPic);
    }

    @Override
    public GoodsPic getIszhutihuakuang(Long centreId) {
        return goodsPicMapper.getIszhutihuakuang(centreId);
    }

}
